From 09666368036df6afe49ec0dc3b71202417263997 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Mon, 29 Jun 2020 19:04:07 +0200 Subject: [PATCH] bitset: Add APIs needed for a filterlistmodel --- docs/reference/gtk/gtk4-sections.txt | 3 ++ gtk/gtkbitset.c | 70 ++++++++++++++++++++++++++++ gtk/gtkbitset.h | 9 ++++ 3 files changed, 82 insertions(+) diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt index 5fefc4d1da..8ec00d44e6 100644 --- a/docs/reference/gtk/gtk4-sections.txt +++ b/docs/reference/gtk/gtk4-sections.txt @@ -355,6 +355,9 @@ gtk_bitset_is_empty gtk_bitset_equals gtk_bitset_get_minimum gtk_bitset_get_maximum +gtk_bitset_get_size +gtk_bitset_get_size_in_range +gtk_bitset_get_nth gtk_bitset_remove_all gtk_bitset_add diff --git a/gtk/gtkbitset.c b/gtk/gtkbitset.c index f2cf9ff446..c69efd0cf7 100644 --- a/gtk/gtkbitset.c +++ b/gtk/gtkbitset.c @@ -193,6 +193,76 @@ gtk_bitset_get_maximum (const GtkBitset *self) return roaring_bitmap_maximum (&self->roaring); } +/** + * gtk_bitset_get_size: + * @self: a #GtkBitSet + * + * Gets the number of values that were added to the set. + * For example, if the set is empty, 0 is returned. + * + * Note that this function returns a #guint64, because when all values are + * set, the return value is #G_MAXUINT + 1. Unless you are sure this cannot + * happen (it can't with #GListModel), be sure to use a 64bit type. + * + * Returns: The number of values in the set. + **/ +guint64 +gtk_bitset_get_size (const GtkBitset *self) +{ + g_return_val_if_fail (self != NULL, 0); + + return roaring_bitmap_get_cardinality (&self->roaring); +} + +/** + * gtk_bitset_get_size_in_range: + * @self: a #GtkBitSet + * @first: the first element to include + * @last: the last element to include + * + * Gets the number of values that are part of the set from @first to @last + * (inclusive). + * + * Note that this function returns a #guint64, because when all values are + * set, the return value is #G_MAXUINT + 1. Unless you are sure this cannot + * happen (it can't with #GListModel), be sure to use a 64bit type. + * + * Returns: The number of values in the set from @first to @last. + **/ +guint64 +gtk_bitset_get_size_in_range (const GtkBitset *self, + guint first, + guint last) +{ + g_return_val_if_fail (self != NULL, 0); + g_return_val_if_fail (last >= first, 0); + + return roaring_bitmap_range_cardinality (&self->roaring, first, ((uint64_t) last) + 1); +} + +/** + * gtk_bitset_get_nth: + * @self: a #GtkBitset + * @nth: index of the item to get + * + * Returns the value of the @nth item in self. + * + * If @nth is >= the size of @self, 0 is returned. + * + * Returns: the value of the @nth item in @self + **/ +guint +gtk_bitset_get_nth (const GtkBitset *self, + guint nth) +{ + uint32_t result; + + if (!roaring_bitmap_select (&self->roaring, nth, &result)) + return 0; + + return result; +} + /** * gtk_bitset_new_empty: * diff --git a/gtk/gtkbitset.h b/gtk/gtkbitset.h index fc26e0d7de..1fcbf6faeb 100644 --- a/gtk/gtkbitset.h +++ b/gtk/gtkbitset.h @@ -48,6 +48,15 @@ GDK_AVAILABLE_IN_ALL gboolean gtk_bitset_equals (const GtkBitset *self, const GtkBitset *other); GDK_AVAILABLE_IN_ALL +guint64 gtk_bitset_get_size (const GtkBitset *self); +GDK_AVAILABLE_IN_ALL +guint64 gtk_bitset_get_size_in_range (const GtkBitset *self, + guint first, + guint last); +GDK_AVAILABLE_IN_ALL +guint gtk_bitset_get_nth (const GtkBitset *self, + guint nth); +GDK_AVAILABLE_IN_ALL guint gtk_bitset_get_minimum (const GtkBitset *self); GDK_AVAILABLE_IN_ALL guint gtk_bitset_get_maximum (const GtkBitset *self); -- 2.30.2